<template>
  <div class="config-group">
    <h4>头部组件配置</h4>
    <!-- 标题文本配置 -->
    <div class="config-section">
      <div class="config-item">
        <label>标题文本：</label>
        <input 
          type="text" 
          v-model="localConfig.text" 
          @input="updateConfig"
          placeholder="请输入标题文本"
        >
      </div>
    </div>

    <!-- 字体相关配置 -->
    <div class="config-section">
      <div class="config-row">
        <div class="config-item flex-item">
          <label>字体：</label>
          <select v-model="localConfig.fontFamily" @change="updateConfig">
            <option value="Arial">Arial</option>
            <option value="Microsoft YaHei">微软雅黑</option>
            <option value="SimHei">黑体</option>
            <option value="SimSun">宋体</option>
            <option value="PingFang SC">苹方</option>
            <option value="YouSheBiaoTiHei">幼圆</option>
          </select>
        </div>
        <div class="config-item flex-item">
          <label>字体大小：</label>
          <input 
            type="text" 
            v-model="localConfig.fontSize" 
            @input="updateConfig"
            placeholder="例如：24px"
          >
        </div>
        <div class="config-item flex-item">
          <label>字间距：</label>
          <input 
            type="text" 
            v-model="localConfig.letterSpacing" 
            @input="updateConfig"
            placeholder="例如：2px"
          >
        </div>
        <div class="config-item flex-item">
          <label>字体粗细：</label>
          <select v-model="localConfig.fontWeight" @change="updateConfig">
            <option value="normal">正常</option>
            <option value="bold">粗体</option>
          </select>
        </div>
        <div class="config-item flex-item">
          <label>文字颜色：</label>
          <el-color-picker 
            v-model="localConfig.color"
            show-alpha
            @change="updateConfig"
            size="small"
            :predefine="predefineColors"
          />
        </div>
        <div class="config-item flex-item">
          <label>层级：</label>
          <input 
            type="number" 
            v-model.number="localConfig.zIndex" 
            @input="updateConfig"
            placeholder="例如：1"
            min="0"
          >
        </div>
      </div>
    </div>

    <!-- 背景相关配置 -->
    <div class="config-section">
      <div class="section-title">背景设置</div>
      
      <!-- 背景图片配置 -->
      <div class="bg-image-config">
        <div class="preview-area">
          <div class="preview-box" :style="previewStyle">
            <span v-if="!localConfig.backgroundImage">暂无图片</span>
          </div>
        </div>
        <div class="image-controls">
          <div class="image-path">
            <span class="path-label">已选图片：</span>
            <span class="path-value">{{ getImageName(localConfig.backgroundImage) || '未选择' }}</span>
          </div>
          <div class="image-actions">
            <ImageSelector 
              v-model="localConfig.backgroundImage"
              @change="handleImageChange"
            />
          </div>
          <div class="image-tips">
              <span class="tips-icon">💡</span>
              <span>图片位于 assets/images 目录下</span>
            </div>
        </div>
      </div>

      <!-- 背景颜色配置 -->
      <div class="config-item">
        <label>背景颜色：</label>
        <el-color-picker 
          v-model="localConfig.backgroundColor"
          show-alpha
          @change="updateConfig"
          size="small"
          :predefine="predefineColors"
        />
      </div>
    </div>
  </div>
</template>

<script>
import ImageSelector from '../../common/ImageSelector.vue'

export default {
  name: 'CommonHeaderConfig',
  components: {
    ImageSelector
  },
  props: {
    config: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      localConfig: {
        ...JSON.parse(JSON.stringify(this.config)),
        text: '大屏页面配置平台',
        fontSize: '60px',
        fontWeight: 'normal',
        fontFamily: 'YouSheBiaoTiHei',
        letterSpacing: '10px',
        color: '#ffffff',
        backgroundImage: require('@/assets/images/header-bg.png'),
        backgroundColor: '#ffffff',
        zIndex: 1
      },
      predefineColors: [
        '#ffffff',
        '#000000',
        '#409EFF',
        '#67C23A',
        '#E6A23C',
        '#F56C6C',
        '#909399',
        'rgba(255, 255, 255, 0.7)',
        'rgba(0, 0, 0, 0.7)'
      ]
    }
  },
  methods: {
    updateConfig() {
      this.$emit('update:config', JSON.parse(JSON.stringify(this.localConfig)))
    },
    handleImageChange(imagePath) {
      this.localConfig.backgroundImage = imagePath
      this.updateConfig()
    },
    getImageName(path) {
      if (!path) return '';
      return path.split('/').pop();
    }
  },
  watch: {
    config: {
      handler(newConfig) {
        this.localConfig = {
          ...JSON.parse(JSON.stringify(newConfig)),
          text: newConfig.text || '大屏页面配置平台'
        }
      },
      deep: true
    }
  },
  computed: {
    previewStyle() {
      return {
        backgroundImage: this.localConfig.backgroundImage ? `url(${this.localConfig.backgroundImage})` : 'none',
        backgroundColor: this.localConfig.backgroundImage ? 'transparent' : '#f5f5f5'
      }
    }
  }
}
</script>

<style scoped>
.config-group {
  margin-bottom: 20px;
  padding: 15px;
  background: #f9f9f9;
  border-radius: 6px;
}

.config-group h4 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 14px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}

.config-section {
  margin-bottom: 20px;
  padding: 15px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.config-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}

.config-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.config-item.half {
  flex: 1;
  min-width: 0;
  gap: 8px;
}

.config-item label {
  min-width: 70px;
  color: #666;
  flex-shrink: 0;
  font-size: 13px;
}

.config-item input:not([type="color"]),
.config-item select {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  min-width: 80px;
  width: 100%;
  font-size: 13px;
}

.config-item input[type="color"] {
  min-width: 50px;
  width: 50px;
  height: 28px;
  padding: 0;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.section-title {
  font-size: 13px;
  color: #333;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px dashed #eee;
}

.bg-image-config {
  margin-bottom: 15px;
  background: #f9f9f9;
  border-radius: 4px;
  padding: 12px;
}

.preview-area {
  margin-bottom: 12px;
}

.preview-box {
  width: 100%;
  height: 120px;
  border: 2px dashed #ddd;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  color: #999;
  font-size: 13px;
}

.image-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.image-path {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.path-label {
  color: #666;
  white-space: nowrap;
}

.path-value {
  color: #1890ff;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.image-tips {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #999;
  font-size: 12px;
}

.tips-icon {
  font-size: 14px;
}

/* 覆盖 ImageSelector 组件的样式 */
:deep(.scan-btn) {
  height: 32px;
  padding: 0 12px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

:deep(.scan-btn:hover) {
  background: #40a9ff;
}

/* 专门针对字体粗细的下拉框 */
.config-item select {
  padding-right: 25px;
  appearance: none;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23666' d='M6 8.825L1.175 4 2.238 2.938 6 6.7l3.763-3.762L10.825 4z'/%3E%3C/svg%3E") no-repeat right 8px center;
}

/* 优化字体选择下拉框 */
.config-item select[v-model="localConfig.fontFamily"] {
  width: 100%;
  min-width: 120px;
}

.config-item.flex-item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 180px; /* 设置最小宽度 */
  flex: 1;
}

.config-item.flex-item label {
  width: auto;
  min-width: 60px;
  white-space: nowrap;
  color: #666;
  font-size: 13px;
}

.config-item.flex-item input:not([type="color"]),
.config-item.flex-item select {
  flex: 1;
  min-width: 80px;
  padding: 4px 8px;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 13px;
}

.config-item.flex-item input[type="color"] {
  width: 50px;
  height: 32px;
  padding: 2px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

/* 优化下拉框样式 */
.config-item.flex-item select {
  padding-right: 25px;
  appearance: none;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23666' d='M6 8.825L1.175 4 2.238 2.938 6 6.7l3.763-3.762L10.825 4z'/%3E%3C/svg%3E") no-repeat right 8px center;
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .config-item.flex-item {
    min-width: calc(33.33% - 10px);
  }
}

@media screen and (max-width: 768px) {
  .config-item.flex-item {
    min-width: calc(50% - 10px);
  }
}

.config-item.flex-item :deep(.el-color-picker) {
  margin-left: 0;
}

.config-item.flex-item :deep(.el-color-picker__trigger) {
  height: 32px;
  width: 60px;
  border: 1px solid #ddd;
  padding: 2px;
}

.config-item :deep(.el-color-picker__trigger) {
  border: 1px solid #ddd;
}
</style> 